import axios from 'axios'
// import router from '@/router'
import {
  MessageBox,
  Message,
  Loading
} from 'element-ui'
import store from '@/store'
import {
  setToken,
  getToken
} from '@/utils/auth'
import {
  showLoading,
  closeLoading
} from '@/utils/loading'

const base_url = process.env.VUE_APP_BASE_URL
// console.log('base_url', base_url)
// create an axios instance
const service = axios.create({
  baseURL: base_url,
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 100 * 1000 // request timeout 60秒
})
// request interceptor
service.interceptors.request.use(config => {
    // if (config.method === 'post') {
    //   if (!config.headers) {
    //     config.headers['Content-Type'] =
    //       'application/json;charset=utf-8'
    //   }
    // } else {
    //   config.headers['Content-Type'] =
    //     'application/x-www-form-urlencoded;charset=utf-8'
    // }
    // config.headers['Cache-Control'] = 'no-cache'
    // config.headers['Pragma'] = 'no-cache'
    // do something before request is sent

    // if (typeof (config.headers) === 'undefined') {
    //   // console.log(config.headers['Content-Type'])
    //   // console.log(111)
    //   config.headers['Content-Type'] =
    //     'application/x-www-form-urlencoded;charset=utf-8'
    // }
    // if (store.getters.token) {
    //   // let each request carry token
    //   // ['X-Token'] is a custom headers key
    //   // please modify it according to the actual situation
    //   if (config.url !== '/common/func/new/message/flag') {
    //     config.headers['X-Auth-Token'] = getToken()
    //   }
    // }
    // console.log(config.noloading, config.url);
    // 防止IE浏览器get请求缓存
    // if (config.method === 'get' && config.params) {
    //   config.params._t = Date.now()
    // } else if (config.method === 'get' && !config.params) {
    //   if (config.url.indexOf('?') === -1) {
    //     config.url = config.url + '?_=' + (new Date().getTime())
    //   } else {
    //     config.url = config.url + '&_=' + (new Date().getTime())
    //   }
    // }
    if (config.url.indexOf('buvDisFiPosController.do?bu00Bu01Waring') != -1) {
      closeLoading()
    } else if (!config.noloading) {
      showLoading()
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const {
      responseType
    } = response.config
    // if (response.config && response.config.url.indexOf('personnelController.do?datagrid') == -1) {
    //   closeLoading()
    // }
    if (response.config) {
      closeLoading()
    }
    // 流文件
    if (responseType === 'blob' || responseType === 'arraybuffer') {
      return response
    }
    // 普通数据
    // return response.data
    if (response.data.code !== 200) {
      Message({
        message: response.data.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(new Error(response.data.message || 'Error'))
    } else {
      return response.data
      // const token = response.headers['x-auth-token']
      // if (token) {
      //   res.token = token
      // }
      // return res
    }
  },
  async error => {
    // console.log('err' + error) // for debug
    // const { status } = error.response
    // if (status && status === 401) {
    //   await store.dispatch('user/logout')
    //   Message({
    //     message: '登录信息已过期',
    //     type: 'error',
    //     duration: 5 * 1000
    //   })
    // } else {
    //   Message({
    //     message: error.message,
    //     type: 'error',
    //     duration: 5 * 1000
    //   })
    // }
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    closeLoading()
    return Promise.reject(error)
  }
)

export default service